<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加角色</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
  <script src="/static/layui/layui.js"></script>
</head>
<body>
    <div class="layui-container" style="padding-top:30px;">
      <input type="hidden" id="role_id" value="{$role_id}">
      <input type="hidden" id="data" value="{$data}">
      <input type="hidden" id="ids" value="{$idArr}">
      <div id="tree"></div>
      <a class="layui-btn layui-btn-fluid" id="submit" style="margin-top: 30px;">立即提交</a>
    </div>
   
    <script>
      layui.use(['form','tree'], function(){
        var form = layui.form,$ = layui.jquery,tree = layui.tree;
        let data = JSON.parse($('#data').val());
        tree.render({
               elem: '#tree',
               data: data,
               showCheckbox: true,
               id: 'treeId'
        })
        let idArr = JSON.parse($('#ids').val());
        tree.setChecked('treeId', idArr)
   
        $('#submit').click(function(){
           let menus = [];
           let role_id = $('#role_id').val();
           let checkbox = $(':checkbox');
           checkbox.each(function(){
               let status =  $(this).prop('checked');
               if(status){
                  menus.push({menu_id:$(this).val(),role_id:role_id});
               }
           });

           if(menus.length<1){
              layer.msg('请选择权限!',{icon:2});
              return false;
           }

           $.post('/admin/role/allow',{role_id:role_id,menus:menus},function(res){
                if(res.code ==1) {
                   layer.msg(res.msg,{icon:1});
                   let index =  parent.layer.getFrameIndex(window.name);
                   parent.layer.close(index);
                   return false;
                }
                layer.msg(res.msg,{icon:2});
            }); 
        });
      });
    </script>
</body>
</html>
